package in.iqing.control.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;

import java.text.SimpleDateFormat;
import java.util.Collection;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.control.helper.ImageResourceHelper;
import in.iqing.model.bean.Book;
import in.iqing.model.data.UniqueList;

/**
 * Created by ZuoShu on 15/7/7.
 */
public class BookListAdapter extends UltimateViewAdapter {
    private UniqueList<Book> bookList;
    private Transformation coverTransformation;
    private Context context;
    private OnItemClickListener listener;
    private SimpleDateFormat timeFormat;

    public interface OnItemClickListener {
        public void onItemClick(Book book);
    }

    public BookListAdapter(Context context) {
        this.context = context;
        bookList = new UniqueList<>();
        coverTransformation = new RoundedTransformationBuilder()
                .cornerRadius(context.getResources().getDimension(R.dimen.round_image_radius))
                .borderWidth(context.getResources().getDimension(R.dimen.image_border_width))
                .borderColor(context.getResources().getColor(R.color.iqing_yellow))
                .oval(false)
                .build();
        timeFormat = new SimpleDateFormat(context.getString(R.string.common_update_date_format));
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void clear() {
        synchronized (bookList) {
            bookList.clear();
        }
    }

    public void add(Collection<Book> books) {
        synchronized (bookList) {
            bookList.addAll(books);
        }
    }

    @Override
    public UltimateRecyclerviewViewHolder onCreateViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public int getAdapterItemCount() {
        return bookList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(UltimateRecyclerviewViewHolder holder, int position) {
        if (position < getItemCount()
                && (customHeaderView != null ? position <= bookList.size() : position < bookList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            ViewHolder customHolder = (ViewHolder) holder;

            Book book = bookList.get(customHeaderView != null ? position - 1 : position);
            RequestCreator createor = TextUtils.isEmpty(book.getCover())
                    ? Picasso.with(context).load(R.drawable.image_loading_1x1)
                    : Picasso.with(context).load(ImageResourceHelper.generateCoverThumbnailImageUrl(book.getCover()));
            createor.placeholder(R.drawable.image_loading_1x1)
                    .error(R.drawable.image_error)
                    .transform(coverTransformation)
                    .resizeDimen(R.dimen.list_cover_width, R.dimen.list_cover_height)
                    .centerCrop()
                    .into(customHolder.cover);
            customHolder.title.setText(book.getTitle());
            customHolder.views.setText(String.valueOf(book.getViews()));
            customHolder.press.setText(book.getPress());
            customHolder.author.setText(book.getAuthor());
            customHolder.update.setText(timeFormat.format(book.getUpdatedTime()));
            ((ViewHolder) holder).book = book;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        // no header
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        // no header
    }

    class ViewHolder extends UltimateRecyclerviewViewHolder {
        @Bind(R.id.cover_image)
        ImageView cover;

        @Bind(R.id.title_text)
        TextView title;

        @Bind(R.id.views_text)
        TextView views;

        @Bind(R.id.press_text)
        TextView press;

        @Bind(R.id.author_text)
        TextView author;

        @Bind(R.id.update_text)
        TextView update;

        Book book;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.book_item_layout)
        public void onItemClick(View v) {
            if (listener != null) {
                listener.onItemClick(book);
            }
        }
    }
}
